package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.adapter.MachineInsAdapter;
import com.mxnet.chongjin.supoman.bean.MachineInstruction;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.fragment.InstructionFragment;
import com.mxnet.chongjin.supoman.fragment.OperateVideoFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class MachineInstructionActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private ViewPager mViewPager;
    private LinearLayout mLayout;
    private TabLayout mTabLayout;
    private MachineInsAdapter mInsAdapter;
    private FragmentManager mFragmentManager;
    private List<Fragment> mFragmentList;
    private InstructionFragment mInstructionFragment;
    private OperateVideoFragment mOperateVideoFragment;
    private TabLayout.Tab mTab;
    private List<String> mList;
    private String robotNo;
    private MachineInstruction machineInstruction;
    private SharedPreferences mPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machine_instruction);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = MachineInstructionActivity.this;
        Intent intent = getIntent();
        robotNo = intent.getStringExtra("key");
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);

        initView();
        initData();
    }

    public void initData() {
        mFragmentManager = getSupportFragmentManager();
        mList = new ArrayList<>();
        mList.add(getResources().getString(R.string.server_use_ins));
        mList.add(getResources().getString(R.string.server_operate_video));
        mFragmentList = new ArrayList<>();
        String country = "";
        switch (mPreferences.getString("lanuage", "de")) {
            case "en":
                country = "English";
                break;
            case "de":
                country = "Genman";
                break;
            case "fr":
                country = "French";
                break;
            case "sv":
                country = "Swedish";
                break;
        }

        OkHttpUtils.get()
                .url(Api.URL_HEADER + Api.MACHINE_TYPE)
                .addParams("robotNo", robotNo)
                .addParams("languages", country)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                machineInstruction = new MachineInstruction();
                                JSONObject jsonObject = new JSONObject(response);
                                JSONObject data = jsonObject.optJSONObject("data");
                                machineInstruction.setRobotNo(data.optString("robotNo"));
                                machineInstruction.setBookUrlId(data.optString("bookUrlId"));
                                if (!data.isNull("bookResource")) {
                                    JSONObject object = data.optJSONObject("bookResource");
                                    MachineInstruction.BookResourceBean bookResourceBean = new MachineInstruction.BookResourceBean();
                                    bookResourceBean.setCreateTime(object.optLong("createTime"));
                                    bookResourceBean.setFileType(object.optString("fileType"));
                                    bookResourceBean.setId(object.optString("id"));
                                    bookResourceBean.setMd5(object.optString("md5"));
                                    bookResourceBean.setName(object.optString("name"));
                                    bookResourceBean.setSize(object.optString("size"));
                                    bookResourceBean.setType(object.optString("type"));
                                    bookResourceBean.setUrl(object.optString("url"));
                                    bookResourceBean.setUserId(object.optString("userId"));
                                    machineInstruction.setBookResource(bookResourceBean);
                                }
                                if (!data.isNull("videoResource")) {
                                    JSONObject object1 = data.optJSONObject("videoResource");
                                    MachineInstruction.VideoResourceBean videoResourceBean = new MachineInstruction.VideoResourceBean();
                                    videoResourceBean.setCreateTime(object1.optLong("createTime"));
                                    videoResourceBean.setFileType(object1.optString("fileType"));
                                    videoResourceBean.setId(object1.optString("id"));
                                    videoResourceBean.setMd5(object1.optString("md5"));
                                    videoResourceBean.setName(object1.optString("name"));
                                    videoResourceBean.setSize(object1.optString("size"));
                                    videoResourceBean.setType(object1.optString("type"));
                                    videoResourceBean.setUrl(object1.optString("url"));
                                    videoResourceBean.setUserId(object1.optString("userId"));
                                    machineInstruction.setVideoResource(videoResourceBean);
                                }
                                machineInstruction.setId(data.optString("id"));
                                machineInstruction.setVideoUrlId(data.optString("videoUrlId"));
                                machineInstruction.setRobotImgUrl(data.optString("robotImgUrl"));

                                Bundle bundle = new Bundle();
                                mInstructionFragment = new InstructionFragment();
                                if (machineInstruction.getBookResource() == null) {
                                    bundle.putString("book", "");
                                } else {
                                    bundle.putString("book", machineInstruction.getBookResource().getUrl());
                                }
                                mInstructionFragment.setArguments(bundle);
                                mFragmentList.add(mInstructionFragment);

                                mOperateVideoFragment = new OperateVideoFragment();
                                if (machineInstruction.getVideoResource() == null) {
                                    bundle.putString("video", "");
                                } else {
                                    bundle.putString("video", machineInstruction.getVideoResource().getUrl());
                                }
                                mOperateVideoFragment.setArguments(bundle);
                                mFragmentList.add(mOperateVideoFragment);

                                mInsAdapter = new MachineInsAdapter(mFragmentManager, mFragmentList);
                                mViewPager.setAdapter(mInsAdapter);

                                for (int i = 0; i < mFragmentList.size(); i++) {
                                    mTab = mTabLayout.getTabAt(i);
                                    mTab.setText(mList.get(i));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    public void initView() {
        mLayout = findViewById(R.id.ll_machine_instruction);
        mLayout.setOnClickListener(this);

        mViewPager = findViewById(R.id.vp_machine_ins);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        
                        break;
                    case 1:

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mTabLayout = findViewById(R.id.tab_machine_ins);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_machine_instruction:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }
}
